package pack01_zookeeper_api;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.TreeCache;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
import org.apache.curator.framework.recipes.cache.TreeCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.ls.LSException;

/**
 * @ClassName ZooKeeperDemo1
 * @Description JavaLotus
 * @Author spring
 * @Date 2024/11/11 19:59
 * @Version 1.0
 **/
public class ZooKeeperDemo1 {

    // 抽取类和引用对象

    CuratorFramework client;

    // 初始化

    @Before
    public void init() {

        // 重试策略
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 3);

        // 客户端对象
        String serverList = "node1:2181,node2:2181,node3:2181";
        client = CuratorFrameworkFactory.newClient(serverList, retryPolicy);

        // 开启客户端
        client.start();

    }

    // 结束关闭

    @After
    public void close() {

        // 关闭客户端
        client.close();
    }

    /**
     * Znode 的 watch 机制
     */
    @Test
    public void test5ZnodeWatch() throws Exception {

        TreeCache treeCache = new TreeCache(client, "/app1");

        treeCache.getListenable().addListener(new TreeCacheListener() {
            @Override
            public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent treeCacheEvent) throws Exception {
                switch (treeCacheEvent.getType()){
                    case NODE_ADDED:
                        System.out.println("监控到增加节点事件!");
                        System.out.println("有客户端上线了!");
                        break;
                    case NODE_REMOVED:
                        System.out.println("监控到节点移除事件!");
                        System.out.println("主节点挂掉了!");
                        System.out.println("让备用节点成为新的主节点");
                        break;
                    case NODE_UPDATED:
                        System.out.println("监控到节点修改事件!");
                        break;
                    case CONNECTION_SUSPENDED:
                        break;
                    case CONNECTION_RECONNECTED:
                        break;
                    case CONNECTION_LOST:
                        break;
                    case INITIALIZED:
                        break;
                }
            }
        });

        treeCache.start();

        Thread.sleep(1000000000);
    }

    /**
     * 查找
     */
    @Test
    public void test3GetZnodeData() throws Exception {
        byte[] bytes = client.getData().forPath("/app1");
        String data = new String(bytes);
        System.out.println(data);

    }

    /**
     * 删除
     */
    @Test
    public void test4DeleteZnode() throws Exception{
        client.delete().deletingChildrenIfNeeded().forPath("/app1");
    }

    /**
     * 修改
     */
    @Test
    public void test2SetZnode() throws Exception {
        client.setData().forPath("/app2", "node2".getBytes());
    }


    /**
     * 增加
     */
    @Test
    public void test1CreateZnode() throws Exception {

        // 节点的创建
        client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/app1", "windows_localhost".getBytes());


    }
}
